package com.dx.cms.Test;

import java.sql.*;

/**
 * @Auther: zephyrus
 * @Date: 2023/11/23
 * @Description:
 * @since
 */
public class TestSelect {
    public static void main(String[] args) throws SQLException {
        //利用jdbc-driver驱动操作数据库
        //1. 加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到驱动");
        }
        Connection conn=null;
        //2. 创建连接
        try {
           conn= DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/jsp","root","root");
        } catch (SQLException e) {
            System.out.println("创建连接失败");
        }
        //3 创建语句对象
//        Statement st=conn.createStatement();
//        ResultSet rs=st.executeQuery("select * from tb_user");
        String username="admin";
        //PreparedStatement ps=conn.prepareStatement("select * from tb_user where username='"+username+"'");
        PreparedStatement ps=conn.prepareStatement("select * from tb_user where username=?");
        ps.setString(1,username);
        //ps.setInt(1,1);
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            System.out.print(rs.getInt("id")+" "
                    +rs.getString("username")+" "
                    +rs.getString("password")+" "
                    +rs.getString("real_name"));
            System.out.println();

        }
        rs.close();
        ps.close();
        conn.close();
    }
}
